package com.cloud.code.算法.深度优先搜索;

import java.util.*;

class Position {
    int x;
    int y;

    Position(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

public class DeepSearch {

    static int[][] d = {{-1, 0}, {1, 0}, {0, 1}};

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()) {

            int n1 = scanner.nextInt();
            int n2 = scanner.nextInt();
            int[][] nums = new int[n1][n2];
            for (int i = 0; i < n1; i++) {
                for (int j = 0; j < n2; j++) {
                    nums[i][j] = scanner.nextInt();
                }
            }

            List<Deque<Position>> list = new ArrayList<>();
            Deque<Position> stack = new ArrayDeque<>();
            boolean[][] used = new boolean[n1][n2];
            int x = 0;
            int y = 0;
            dfs(x, y, nums, stack, list, used);

            for (int i = 0; i < n1; i++) {
                for (int j = 0; j < n2; j++) {
                    System.out.print(nums[i][j]);
                }
                System.out.println();
            }


        }


    }

    private static void dfs(int x, int y, int[][] nums, Deque<Position> stack, List<Deque<Position>> list, boolean[][] used) {

        if (x == nums.length - 1 && y == nums[0].length - 1) {
            list.add(stack);
            return;
        }

        if(used[x][y]){
            return;
        }


        for (int i = 0; i < 3; i++) {
            int newx = x + d[i][0];
            int newy = x + d[i][1];

            if (0 < newx && newx < nums.length && newy < nums[0].length - 1 && nums[newx][newy]!=1) {
                used[newx][newy] = true;
                stack.addLast(new Position(newx, newy));
                dfs(newx, newy, nums, stack, list, used);
                used[newx][newy] = false;
                stack.removeLast();
            }else {
                continue;
            }
        }

        return;
    }
}
